<?php
/**
 * 用户认证类
 *
 * @author <judasnow@gmail.com>
 */
class user_auth{

	private $_CI;
	private $_user;

	public function __construct(){

		$this->_CI =& get_instance();
		require_once( APPPATH . '/libraries/User.php' );
	}

	//判断当前是否已经有用户登录系统
	public function is_login(){
		$user = $this->_CI->session->userdata( 'user' );
		//判断是否 session 中保存的是 user 类型的对象
		if( !empty( $user['user_obj'] ) && ( unserialize( $user['user_obj'] ) instanceof user ) ){
			//session 中保存有 user 信息 证明已经由用户登录
			return TRUE;
		}
		return FALSE;
	}

	/**
	 * 进行用户登录系统的操作
	 * 目前以 oauth 方式进行登录 用户在本站不需要输入任何参数
	 * 此函数会直接定向到 dazery.com 站上的相应页面
	 * 
	 */ 
	public function do_login(){
		 //通过 oauth 获取用户信息
		 //保存在 session 中	
		 $this->_CI->config->load( 'oauth' );
		 $this->_CI->load->library( 'oauth_client' );
		 //oauth 返回的 dazery 上的 user_id 
		 $user = $this->_CI->session->userdata( 'user' );
		 $consumer_user_id = $user['consumer_user_id'] = '71';
		 $server_base_uri = $this->_CI->config->item( 'server_base_uri' );
		 //$request_uri = $server_base_uri . '/api/account/basic/' . $consumer_user_id;
		 //$res = $this->_CI->oauth_client->request( $request_uri , TRUE );
		 $res = '{"email":"ppp@ppp.com","user_id":"71","user_nick":"Apple-Inc","reg_time":"2011-09-04 13:53:57","domain":"AppleInc","contacts_num":4,"rev_contacts_num":7,"solution_num":35,"sys_domain":"3370143595","describe":"Steve Jobs, 1955 - 2011","motto":"\u3010\u4e54\u5e03\u65af10\u5927\u7ba1\u7406\u6212\u5f8b\u30111\u3001\u8ffd\u6c42\u5b8c\u7f8e\u30022\u3001\u5668\u91cd\u4e13\u5bb6\u30023\u3001\u6562\u4e8e\u6b8b\u5fcd\u30024\u3001\u907f\u514d\u7126\u70b9\u5c0f\u7ec4\u30025\u3001\u4e0d\u65ad\u7814\u7a76\u5b66\u4e60\u30026\u3001\u6781\u7b80\u4e3b\u4e49\u30027\u3001\u5b88\u4f4f\u4f60\u7684\u79d8\u5bc6\u30028\u3001\u4fdd\u6301\u5c0f\u7684\u56e2\u961f\u30029\u3001\u591a\u5956\u52b1\u5c11\u60e9\u7f5a\u300210\u3001\u6781\u7aef\u7684\u539f\u578b\u6982\u5ff5\u3002","address":"Apple 1 Infinite Loop Cupertino, CA 95014 408.996.1010 \u82f9\u679c\u4e2d\u56fd\u5730\u5740\uff1a \u5317\u4eac\u5e02\u671d\u9633\u533a\u5efa\u56fd\u95e8\u5916\u5927\u8857\u753212\u53f7 \u65b0\u534e\u4fdd\u9669\u5927\u53a69\u5c42 \u90ae\u7f16\uff1a100022","tel":"1234567","snstools":{"qq":null,"tsina":"apple","tqq":null,"tsohu":null,"t163":null,"custom":"apple.com"},"loc_city":"\u9999\u6e2f\u7279\u522b\u884c\u653f\u533a","loc_province":"\u9999\u6e2f"}';
		 //获得用户 信息数组
		 $user_info = json_decode( $res , TRUE );
		 //如果没有返回用户信息 需要抛出异常
		 if( empty( $res ) ){
			 throw new Exception( '服务端没有返回所期望的用户信息.' );
			 return;
		 }
		 //生成 user 对象，并保存在 session 中
		 $params = array( 'user_info'=>$user_info );
		 $this->_CI->load->library( 'user' , $user_info );
		 $this->_CI->session->set_userdata( 'user' , array( 'user_id'=>$consumer_user_id , 'user_obj'=>serialize( $this->_CI->user ) ) );
		 $this->_CI->load->model( 'user_model' , '' , TRUE );
		 //判断数据库中是否已经有该 user_id 所确定的用户的信息
		 if( !$this->_CI->user_model->is_exists( array( 'user_id'=>$user_info['user_id'] ) ) ){
			 //不存在
			 //保存该用户记录在数据库中		 
			 $res = $this->_CI->user_model->add_new( array( 
				 'user_id'=>$user_info['user_id'] ,
				 'email'=>$user_info['email'] , 
				 'nick'=>$user_info['user_nick']
			 ) );	 
		 }
		header( 'Location: /' );
		exit;
		return TRUE;
	}

	//用户退出登录
	public function do_logout(){
	
		$this->_CI->session->unset_userdata( 'user' );

		//推出登录之后 定向到首页
		header( 'Location: /' );
		exit;
	} 
}
